<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="misc.*,data.File,data.User,controller.FileDeletionController,data.DownloadLog,data.UploadLog,java.util.LinkedList,java.util.ArrayList,java.text.SimpleDateFormat"%>
<%@page import="controller.AdminController"%>
<%@page import="data.IPServing"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.DataInputStream"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.InputStreamReader"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title><%=Configurations.title%>Administration</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<h1>Administration (Secret page)</h1>
<br/>
<div align="left">
<b>Actions:</b> <br/>
<%
if(request.getParameter("delete")!=null)
{
    FileDeletionController this_control = new FileDeletionController();
    int delete = this_control.purgeExpiredFiles();
    %><div class="sucess"><%= delete%> expired files are deleted.</div><%
}
%>
    <form name="input" action="Admin.jsp" method="post">
       <input type="hidden" value="delete" name="delete"/>
       <input type="submit" value="Delete expired files"/>
    </form>
</div> 
<hr/>
<%
AdminController control = new AdminController();
%>
<h2>List of registered users</h2>
    <table class="display">
        <tr class="heading">
          <td>Username</td>
          <td>Date of registration</td>
        </tr>
        <%
        LinkedList<User> users = control.retrieveUser();
        for(int i=0;i<users.size();i++){
            User this_user = users.get(i);
        %>
        <tr class="row">
          <td><%=this_user.getUserName()%></td>
          <td><%=new SimpleDateFormat("dd MMMMM yyyy h:mm:ss a z").format(this_user.getDateRegistration()).toString()%></td>
        </tr>
        <%}%>
    </table>
    <br/>
<hr/>
<h2>List of uploaded files</h2>
    <table class="display">
        <tr class="heading">
          <td>File ID</td>
          <td>Name</td>
          <td>File size</td>
          <td>Filepath</td>
          <td>File type</td>
          <td>Deleted</td>
          <td>Hashed Code</td>
          <td>Password protected</td>
          <td>Hashed Password</td>
        </tr>
        <%
        LinkedList<File> files = control.retrieveFile();
        for(int i=0;i<files.size();i++){
            File this_file = files.get(i);
        %>
        <tr class="row">
          <td><%=this_file.getFileID()%></td>
          <td><%=this_file.getName()%></td>
          <td><%=File.convertToMB(this_file.getSize())%></td>
          <td><%=this_file.getFilepath()%></td>
          <td><%=this_file.getType()%></td>
          <td><%=File.deleteFile(this_file.isDeleted())%></td>
          <td><%=this_file.getHashedCode() %></td>
          <td><%=File.protectedFile(this_file.isProtected())%></td>
          <td><%if(this_file.getHashedPassword()==null){%>None<%}else{%><%=this_file.getHashedPassword()%><%}%></td>
        </tr>
        <%}%>
    </table>
    <br/>
<hr/>
<h2>Download logs</h2>
    <table class="display">
        <tr class="heading">
          <td>ID</td>
          <td>File ID</td>
          <td>IP address</td>
          <td>Start date</td>
          <td>Finished date</td>
          <td>Start time</td>
          <td>Finished time</td>
          <td>Username</td>
        </tr>
        <% 
        LinkedList<DownloadLog> downloads = control.retrieveDownloadLog();
        for(int i=0;i<downloads.size();i++){
            DownloadLog this_download = downloads.get(i);
        %>
        <tr class="row">
          <td><%=this_download.getID()%></td>
          <td><%=this_download.getFileID() %></td>
          <td><%=this_download.getIPaddress() %></td>
          <td><%=new SimpleDateFormat("dd MMMMM yyyy").format(this_download.getStart()).toString() %></td>
          <td><%=new SimpleDateFormat("dd MMMMM yyyy").format(this_download.getFinish()).toString() %></td>
          <td><%=new SimpleDateFormat("h:mm:ss a z").format(this_download.getStart()).toString() %></td>
          <td><%=new SimpleDateFormat("h:mm:ss a z").format(this_download.getFinish()).toString() %></td>
          <td><%if(this_download.getUsername()==null){%>Unregistered<%}else{%><%=this_download.getUsername()%><%}%></td>
        </tr>
        <%}%>
    </table>
    <br/>
<hr/>
<h2>Upload logs</h2>
    <table class="display">
        <tr class="heading">
          <td>ID</td>
          <td>File name</td>
          <td>File ID</td>
          <td>File size</td>
          <td>File type</td>
          <td>IP address</td>
          <td>Start date</td>
          <td>Finished date</td>
          <td>Start time</td>
          <td>Finished time</td>
          <td>Username</td>
          <td>Password protected</td>
        </tr>
        <%
        LinkedList<UploadLog> uploads = control.retrieveUploadLog();
        for(int i=0;i<uploads.size();i++){
            UploadLog this_upload = uploads.get(i);
        %>
        <tr class="row">
          <td><%=this_upload.getID() %></td>
          <td><%=this_upload.getFileName() %></td>
          <td><%=this_upload.getFileID() %></td>
          <td><%=File.convertToMB(this_upload.getFilesize()) %></td>
          <td><%=this_upload.getFiletype() %></td>
          <td><%=this_upload.getIPaddress() %></td>
          <td><%=new SimpleDateFormat("dd MMMMM yyyy").format(this_upload.getStart()).toString() %></td>
          <td><%=new SimpleDateFormat("dd MMMMM yyyy").format(this_upload.getFinish()).toString() %></td>
          <td><%=new SimpleDateFormat("h:mm:ss a z").format(this_upload.getStart()).toString() %></td>
          <td><%=new SimpleDateFormat("h:mm:ss a z").format(this_upload.getFinish()).toString() %></td>
          <td><%if(this_upload.getUsername()==null){%>Unregistered<%}else{%><%=this_upload.getUsername()%><%}%></td>
          <td><%if(!this_upload.isFileIsProtected()){%>Nope<%}else{%><%=this_upload.getFileHashedPassword()%><%}%></td>
        </tr>
        <%}%>  
    </table>
    <br/>
<hr/>
<h2>IP being servings</h2>
<table class="display">
        <tr class="heading">
          <td>IP address</td>
          <td>File ID being downloaded</td>
        </tr>
        <%
        LinkedList<IPServing> servings = control.retrieveIPServings();
        for(int i=0;i<servings.size();i++){
            IPServing this_serving = servings.get(i);
        %>
        <tr class="row">
          <td><%=this_serving.getIPaddress() %></td>
          <td><%=this_serving.getDownloadingFileID() %></td>
        </tr>
        <%}%>  
    </table>
    <br/>
<hr/>
<h2>PendingFile Lists</h2>
    <table class="display">
        <tr class="heading">
          <td>FileID</td>
        </tr>
        <%
        ArrayList<Long> pendingFiles = control.retrievePendingFiles();
        for(int i=0;i<pendingFiles.size();i++){
        %>
        <tr class="row">
          <td><%=pendingFiles.get(i) %></td>
        </tr>
         <%}%>
    </table>
<br/>
<hr/>
<h2>Server logs</h2>
<%
//Display server log
FileInputStream fstream = new FileInputStream(Configurations.LogFile);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String log_line;
while((log_line=br.readLine())!=null)
{
    %><%= log_line%><br/><%
}
in.close();
%>
<br/>
<hr/>
<div align="right">
    <form name="input" action="ManageFiles.jsp" >
       <input type="submit" value="Management Page"/>
    </form>
</div> 
</body>
</html>